/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-6-18 下午4:00 Created by lyyang
 */
package com.jade.bss.business.bonus.impl;

import java.util.Date;
import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.jade.bss.business.bonus.Bonus;
import com.jade.bss.business.bonus.BonusDao;
import com.jade.bss.business.bonus.BonusRecord;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;

/**
 * 红包dao,jpa实现
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-6-18 下午4:00
 */

@Named ("bss_bonusDao")
public class JpaBonusDao
        implements BonusDao
{
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    public void addRecord(BonusRecord record)
    {
        entityManager.persist(record);
    }

    public BonusRecord getRecord(String receiverType, String receiverId, String orderNo)
    {
        String sql = "SELECT record FROM Bonus record WHERE receivedType=:type and receivedId=:id and orderNo=:orderNo";
        Query query = entityManager.createQuery(sql);
        query.setParameter("type", receiverType);
        query.setParameter("id", receiverId);
        query.setParameter("orderNo", orderNo);
        try {
            return (BonusRecord) query.getSingleResult();
        }
        catch (NoResultException ex) {

        }
        return null;
    }

    public BonusRecord getRecord(long id)
    {
        return entityManager.find(BonusRecord.class, id);
    }

    public void updateRecord(BonusRecord record)
    {
        entityManager.merge(record);
    }


    public PagingList<BonusRecord> queryRecord(Criteria criteria)
    {
        String sql = "SELECT record FROM BonusRecord record";
        return SqlUtils.sqlQuery(sql, entityManager, criteria);
    }

    public List<BonusRecord> listRecord(Criteria criteria)
    {
        String sql = "SELECT record FROM BonusRecord record";
        return SqlUtils.sqlQueryWithoutCount(sql, entityManager, criteria);
    }


    public Bonus add(Bonus bonus)
    {
        entityManager.persist(bonus);
        return bonus;
    }

    public Bonus get(long id)
    {
        return entityManager.find(Bonus.class, id);
    }

    public void update(Bonus bonus)
    {
        entityManager.merge(bonus);
    }

    public void remove(Bonus bonus)
    {
        entityManager.remove(bonus);
    }

    public PagingList<Bonus> query(Criteria criteria)
    {
        String sql = "SELECT bonus FROM Bonus bonus";
        return SqlUtils.sqlQuery(sql, entityManager, criteria);
    }

    public List<Bonus> list(Criteria criteria)
    {
        String sql = "SELECT bonus FROM Bonus bonus";
        return SqlUtils.sqlQueryWithoutCount(sql, entityManager, criteria);
    }

    public BonusRecord getRecord(long bonusId, String ownerType, String ownerId)
    {
        String sql = "SELECT record FROM BonusRecord record WHERE bonusId=:id AND receiverId=:ownerId AND receiverType=:ownerType";
        if (bonusId==0){
            sql = "SELECT record FROM BonusRecord record WHERE receiverId=:ownerId AND receiverType=:ownerType";
        }
        Query query = entityManager.createQuery(sql);
        if (bonusId>0){
            query.setParameter("id", bonusId);
        }
        query.setParameter("ownerId", ownerId);
        query.setParameter("ownerType", ownerType);

        List<BonusRecord> records = query.getResultList();
        if (records!=null&&records.size()>0){
            return records.get(0);
        }

        return null;
    }

    public void updateRecordStatus(long id, int status)
    {
        String sql = "UPDATE BonusRecord SET status=:status WHERE id=:id";
        Query query = entityManager.createQuery(sql);
        query.setParameter("status", status);
        query.setParameter("id", id);
        query.executeUpdate();
    }

    public void consume(long id, long amount)
    {
        String sql = "UPDATE Bonus SET totalCost=totalCost+:amount,quantity=quantity-1 WHERE id=:id";
        Query query = entityManager.createQuery(sql);
        query.setParameter("amount", amount);
        query.setParameter("id", id);
        query.executeUpdate();
    }

    public long statBonusRecordCount(long id, int status, Date beginDate, Date endDate)
    {
        String sql = "SELECT count(bonusRecord) FROM BonusRecord bonusRecord WHERE recordTime>=:beginDate and recordTime<:endDate and id=:id and status=:status";
        Query query = entityManager.createQuery(sql);
        query.setParameter("beginDate", beginDate);
        query.setParameter("endDate", endDate);
        query.setParameter("id", id);
        query.setParameter("status", status);
        Long result = (Long) query.getSingleResult();
        if (result != null) {
            return result.longValue();
        }
        else {
            return 0;
        }
    }
}
